package com.future.taglib

import com.diaryBlogModule.DiaryEntity
import com.diaryBlogModule.Diary
import com.diaryBlogModule.DiaryType

class TreeViewTagLib {
  def treeView = {attrs ->
    def diarybook = Diary.get(attrs.diaryId)
    def typelist = DiaryType.executeQuery("from DiaryType")
    out << "<ul id='treeview'>"
    typelist.each {
      def diaryType = it
      def type = it.diaryType
      println diaryType
      out << "<li><span>${type}</span><ul>"

      def yearlist = DiaryEntity.executeQuery("select  year from DiaryEntity d where  d.diary.diaryType=? group by year order by year asc", [diaryType])
      println type+":"+yearlist
      yearlist.each {
        def year = it
        out << "<li><span>${year}年</span><ul>"
        def monthlist = DiaryEntity.executeQuery("select month from DiaryEntity d where d.year=?  and d.diary.diaryType=? group by month order by month asc", [year, diaryType])
        monthlist.each {
          def month = it
          out << "<li><span><a href='#'>${month}月</a></span><ul>"
          def daylist = DiaryEntity.executeQuery("select  day from DiaryEntity d where d.year=? and d.month=? and d.diary.diaryType=? group by day order by day asc", [year, month,diaryType])
          daylist.each {
            def day = it
            out << "<li><span><a href='#'>${day}日</a></span><ul>"
            def diaryEntityList = DiaryEntity.executeQuery("from DiaryEntity d where d.year=? and d.month=? and d.day=?  and d.diary.diaryType=?", [year, month, day,diaryType])
            diaryEntityList.each {
              def diary = it
              out << "<li><span><a href='#' onclick='showDiary(${diary.id})'>${diary.diaryName}</a></span></li>"
            }
            out << "</ul></li>"
          }
          out << "</ul></li>"
        }
        out << "</ul></li>"
      }
      out << "</ul></li>"
    }
    out << "</ul>"
  }
}
